<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="keyWord"></el-input>
      <el-button @click="clearTask">清除</el-button>
      <el-button type="primary" @click="searchKeyWord">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{row}">
          <el-button size="mini" style="margin-right: 10px" @click="changetask(row.id)">编辑</el-button>
          <el-popconfirm @confirm="delconfirm(row.id)" confirmButtonText="好的" cancelButtonText="不用了" icon="el-icon-info" iconColor="red" title="确定删除任务吗？">
            <template #reference>
              <el-button size="mini" type="danger" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next" :total="total" :page-size="params._limit"  @current-change="getList"> </el-pagination>
      <el-button size="small" plain @click="addtask">+ 新增</el-button>
    </div>
    <!-- 弹框 -->
    <el-dialog title="提示" :visible="dialogVisible" width="30%" @close="cancelDialog">
      <el-form :rules="rules" :model="addForm" ref="form">
        <el-form-item label="任务名字" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="addForm.des"></el-input>
        </el-form-item>
      </el-form>
      <span class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
import { getListAPI, addtaskAPI, deltaskAPI, updatataskAPI, changetaskAPI, searchTaskAPI } from '@/api/api'
export default {
  data () {
    return {
      list: [],
      filterlist: [],
      total: 0,
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      dialogVisible: false,
      addForm: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '名称必填', trigger: 'blur' }]
      },
      keyWord: ''
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    async getList (val) {
      this.params._page = val
      const res = await getListAPI(this.params)
      this.total = Number(res.headers['x-total-count'])
      this.list = res.data
      console.log(this.list)
    },
    addtask () {
      // 1.打开弹框
      this.dialogVisible = true
    },
    cancelDialog () {
      this.resetform()
      // this.addForm.id = ''
      this.dialogVisible = false
    },
    confirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.addForm.id) {
            console.log(this.addForm.id)
            // 根性接口
            await changetaskAPI(this.addForm)
            this.$message.success('编辑成功')
          } else {
            // 添加接口s
            this.$message.success('添加成功')
            this.addForm.id = uuidv4()
            await addtaskAPI(this.addForm)
          }
        }
        this.cancelDialog()
        this.getList()
        // this.$message.success(`${this.addForm.id ? '编辑' : '新增'}chenggong`)
      })
    },
    async delconfirm (id) {
      await deltaskAPI(id)
      this.getList()
    },
    async changetask (id) {
      this.dialogVisible = true
      const res = await updatataskAPI(id)
      this.addForm = res.data
      console.log(res.data)
    },
    // 清空列表
    resetform () {
      // 清空校验规则
      this.$refs.form.resetFields()
      this.addForm = {
        name: '',
        des: '',
        id: ''
      }
    },
    async searchKeyWord () {
      const res = await searchTaskAPI(this.keyWord)
      console.log(res)
      this.list = res.data
    },
    clearTask () {
      this.keyWord = ''
      this.getList()
    }
  }
  // watch: {
  //   keyWord (val) {
  //     // console.log('keyword是', val)
  //     this.filterlist = this.list.filter((item) => {
  //       return item.name.indexOf(val) !== -1
  //     })
  //   }
  // }
  // computed: {
  //   filterlist () {
  //     return this.list.filter((item) => {
  //       return item.name.indexOf(this.keyWord) !== -1
  //     })
  //   }
  // }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
